package tech.dx.springdb.controller;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tech.dx.springdb.entity.CourseCategories;
import tech.dx.springdb.service.CourseCategoriesService;

import java.util.Optional;

/**
 * @ClassName CourseCategoriesController
 * @Description TODO
 * @Author daoxuan
 * @Date 2019/4/2 18:07
 * @Version 1.0
 */
@RestController
@RequestMapping("coursecategories")
@Api(tags = {"课程类别  表示都有什么课"})
public class CourseCategoriesController {
    @Autowired
    private CourseCategoriesService courseCategoriesService;

    @GetMapping("/{id}")
    public CourseCategories get(@PathVariable long id) {
        Optional<CourseCategories> byId = courseCategoriesService.getById(id);
        if (byId.isPresent()) {
            return byId.get();
        } else {
            return null;
        }
    }

    @PostMapping
    public CourseCategories add(@RequestBody CourseCategories courseAdviceType) {
        return courseCategoriesService.add(courseAdviceType);
    }

    @PutMapping("/{id}")
    public CourseCategories update(@RequestBody CourseCategories courseCategories, @PathVariable long id) {
        if (courseCategoriesService.isExitById(id)) {
            courseCategories.setId(id);
            return courseCategoriesService.update(courseCategories);
        } else {
            return null;
        }
    }

    @DeleteMapping("/{id}")
    public void del(@PathVariable long id) {
        if (courseCategoriesService.isExitById(id)) {
            courseCategoriesService.del(id);
        }
    }
}
